package com.ynet.middleground.user.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.BaseReqObjPager;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.AddChannelResourceReq;
import com.ynet.middleground.user.bean.QueryChannelResourceReq;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.dto.ChannelResourcesDTO;
import com.ynet.middleground.user.model.ChannelResourceModel;
import com.ynet.middleground.user.service.ChannelResourceService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 * @description 渠道资源配置管理服务
 * @date 2020-07-07 17:43
 */
@Api(value = "跨渠道资源配置管理服务", tags = {"跨渠道资源配置管理服务"})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Path("channel-resources")
@Service(timeout = 6000, version = "0.0.1")
public class ChannelResourceServiceImpl implements ChannelResourceService, GenericService {

    @Autowired
    private ChannelResourceModel channelResourceModel;

    /**
     * 新增或修改跨渠道资源配置，支持批量
     *
     * @param req 新增跨渠道资源配置请求参数
     * @return 结果以 success 字段为准, true 为成功， false 为失败
     */
    @ApiOperation(value = "新增或修改跨渠道资源配置", notes = "新增或修改跨渠道资源配置，支持批量", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "AddChannelResourceReq", paramType = "body", required = true)
    @POST
    @Override
    public ServiceResult<Object> saveOrUpdate(@Valid AddChannelResourceReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            channelResourceModel.saveOrUpdate(req);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "新增或修改跨渠道资源配置服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 根据原渠道号和模板渠道号查询跨渠道资源信息
     *
     * @param req 查询跨渠道资源配置详情请求参数
     * @return 跨渠道资源配置传输对象
     */
    @ApiOperation(value = "根据原渠道号和模板渠道号查询跨渠道资源信息", notes = "根据原渠道号和模板渠道号查询跨渠道资源信息", hidden = true)
    @Override
    public ServiceResult<ChannelResourcesDTO> getDetail(@Valid QueryChannelResourceReq req) {
        ServiceResult<ChannelResourcesDTO> result = new ServiceResult<>();
        try {
            result.setResult(channelResourceModel.getDetail(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(),
                "根据原渠道号和模板渠道号查询跨渠道资源信息服务异常，case: {}", Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 根据原渠道号和模板渠道号删除两者之间跨渠道所有资源信息
     *
     * @param req 查询跨渠道资源配置详情请求参数
     * @return 结果以 success 字段为准, true 为删除成功， false 为删除失败
     */
    @ApiOperation(value = "根据原渠道号和模板渠道号删除两者之间跨渠道所有资源信息", notes = "根据原渠道号和模板渠道号删除两者之间跨渠道所有资源信息", hidden = true)
    @Override
    public ServiceResult<Object> delete(@Valid QueryChannelResourceReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            channelResourceModel.delete(req);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(),
                "根据原渠道号和模板渠道号删除两者之间跨渠道所有资源信息服务异常，case: {}", Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 分页查询跨渠道信息
     *
     * @param req 基础参数分页
     * @return 跨渠道资源配置列表
     */
    @ApiOperation(value = "分页查询跨渠道信息", notes = "分页查询跨渠道信息", hidden = true)
    @Override
    public ServiceResult<List<ChannelResourcesDTO>> listPage(@Valid BaseReqObjPager req) {
        ServiceResult<List<ChannelResourcesDTO>> result = new ServiceResult<>();
        try {
            result.setResult(channelResourceModel.listPage(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "分页查询跨渠道信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        result.setPagerInfo(req.getPagerInfo());
        return result;
    }

    /**
     * 同步跨渠道资源信息至 redis
     *
     * @return 结果以 success 字段为准, true 为执行成功， false 为执行失败
     */
    @ApiOperation(value = "同步跨渠道资源信息至 redis", notes = "同步跨渠道资源信息至 redis", hidden = true)
    @Override
    public ServiceResult<Object> doSyncChannelResourceToRedis() {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            channelResourceModel.doSyncChannelResourceToRedis();
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "同步跨渠道资源信息至服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

}
